import { getUserAll } from '@/api/users'
import { get } from 'lodash';

const userList = {
  namespaced: true,
  state: {
    loading: false,
    userList: [],
  },

  mutations: {
    SAVE_USER_LIST: (state, payload) => {
      state.userList = payload;
    },
    TOGGLE_LOADING: (state, payload) => {
      state.loading = payload
    },
  },

  actions: {
    async fetchUserList({ commit }, payload) {
      commit('TOGGLE_LOADING', true);
      try {
        const { data } = await getUserAll();
        commit('SAVE_USER_LIST', get(data, 'data', []));
      } catch (err) {
        console.error(err)
      }
      commit('TOGGLE_LOADING', false);
    },
  }
}

export default userList
